package cn.dm.example.aqs;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;

@Slf4j
public class FutureTaskExample {

    public static void main(String[] args) throws Exception {
        FutureTask<String> futureTask = new FutureTask<String>(new Callable<String>() {
            @Override
            public String call() throws Exception {
                log.info("do something in callable");
                Thread.sleep(5000);
                return "Done";
            }
        });

        new Thread(futureTask).start();
        log.info("do something in main");
        Thread.sleep(1000);
        String result = futureTask.get();
        log.info("result：{}", result);
        /**
         * 结果
         * 22:11:23.309 [Thread-0] INFO cn.dm.example.aqs.FutureTaskExample - do something in callable
         * 22:11:23.309 [main] INFO cn.dm.example.aqs.FutureTaskExample - do something in main
         * 22:11:28.310 [main] INFO cn.dm.example.aqs.FutureTaskExample - result：Done
         *
         * 看时间,从23到28确实是阻塞了5s
         */
    }
}
